package com.demo.admin;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.demo.common.model.Content;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;
import com.demo.common.model.Category;

public class ContentValidator extends Validator{
	Category category = new Category();
	protected void validate(Controller controller) {
		validateRequiredString("content.title", "titleMsg", "请输入标题!");
	}
	protected void handleError(Controller controller) {
		controller.keepModel(Content.class);
		
		String actionKey = getActionKey();
		if (actionKey.equals("/admin/savecontent")){
			List<Category> categorylist = category.getCategory();
			controller.setAttr("categorylist",categorylist);		
			Date date=new Date();
			DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time=format.format(date);
			controller.setAttr("time",time); 
			controller.setAttr("form_display","form_display"); 
			controller.render("form.html");
		}
		else if (actionKey.equals("/admin/updateArticle")){
			List<Category> categorylist = category.getCategory();
			controller.setAttr("categorylist",categorylist);		
			Date date=new Date();
			DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String time=format.format(date);
			controller.setAttr("time",time); 
			controller.setAttr("table_display","table_display"); 
			controller.render("editArticle.html");
		}
			
		
	}
}
